//
//  RealTimeCell.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "RealTimeCell.h"
#import "ProgressView.h"

@interface RealTimeCell()
@property (nonatomic, strong)UIImageView *expressionImageView;//表情
@property (nonatomic, strong)UIImageView *headImage;//头像
@property (nonatomic, strong)UILabel *subTitleLabel;//副标题
@property (nonatomic, strong)UILabel *industryLabel;//行业展示
@property (nonatomic, strong)UIImageView *sexImageView;//性别头像
@property (nonatomic, strong)UIButton *normalButton;//基本状态
@property (nonatomic, strong)UIButton *talkAttackButton;//基本状态
@property (nonatomic, strong)ProgressView *progressView;
@property (nonatomic, strong)UIImageView *customerImageView;
@property (nonatomic, strong)UIButton *serviceCustomerButton;//接客
@property (nonatomic, assign)NSInteger bageCount;//
@end

@implementation RealTimeCell

- (UIImageView *)expressionImageView
{
    if (!_expressionImageView)
    {
        UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,20*kUIScaleBasePlus,80*kUIScaleBasePlus,16*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x000000)
                                                   textFont:17*kUIScaleBasePlus];
        titleLabel.text = @"来客啦！";
        [self.contentView addSubview:titleLabel];
        
        _expressionImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)-13.5*kUIScaleBasePlus,19.5*kUIScaleBasePlus,17.5*kUIScaleBasePlus,17.5*kUIScaleBasePlus)];
        _expressionImageView.layer.masksToBounds = YES;
        _expressionImageView.layer.cornerRadius = _headImage.frame.size.width/2;
//        _expressionImageView.backgroundColor = JSAMainColor;
    }
    return _expressionImageView;
}

- (UIImageView *)headImage
{
    if (!_headImage)
    {
        
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15*kUIScaleBasePlus,CGRectGetMaxY(self.expressionImageView.frame)+20*kUIScaleBasePlus,110*kUIScaleBasePlus,110*kUIScaleBasePlus)];
        bgImageView.image = [UIImage imageNamed:@"photoBG"];
        [self.contentView addSubview:bgImageView];
        
        _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(20*kUIScaleBasePlus,CGRectGetMaxY(self.expressionImageView.frame)+25*kUIScaleBasePlus,100*kUIScaleBasePlus,100*kUIScaleBasePlus)];
//        _headImage.layer.masksToBounds = YES;
//        _headImage.layer.cornerRadius = 6;
//        _headImage.backgroundColor = JSAMainColor;
    }
    return _headImage;
}

- (UILabel *)subTitleLabel
{
    if (!_subTitleLabel)
    {
        _subTitleLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.expressionImageView.frame)+3.5*kUIScaleBasePlus,23*kUIScaleBasePlus,40*kUIScaleBasePlus,10.5*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentLeft
                                              textClor:LCHexColor(0x000000)
                                              textFont:11*kUIScaleBasePlus];;
    }
    return _subTitleLabel;
}

- (UIImageView *)sexImageView
{
    if (!_sexImageView)
    {
        _sexImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.subTitleLabel.frame)+21*kUIScaleBasePlus,20.5*kUIScaleBasePlus,15*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    }
    return _sexImageView;
}

- (UIButton *)normalButton
{
    if (!_normalButton)
    {
        _normalButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.sexImageView.frame)+5*kUIScaleBasePlus,22.5*kUIScaleBasePlus,75*kUIScaleBasePlus,11.5*kUIScaleBasePlus)
                                          textAligment:UIControlContentHorizontalAlignmentLeft
                                                  text:@"性别"
                                              textClor:LCHexColor(0x282828)
                                             buttonBGC:[UIColor clearColor]
                                              textFont:11*kUIScaleBasePlus
                                                target:self
                                                method:@selector(normalClcik)];
        _normalButton.enabled = NO;
    
    }
    return _normalButton;
}

- (UILabel *)industryLabel
{
    if (!_industryLabel)
    {
        _industryLabel = [Utilties creatLabelWithFrame:CGRectMake(SCREEN_WIDTH-35*kUIScaleBasePlus-80*kUIScaleBasePlus,22.5*kUIScaleBasePlus,80*kUIScaleBasePlus,11.5*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentRight
                                              textClor:LCHexColor(0x29B7A3)
                                              textFont:12*kUIScaleBasePlus];
    }
    return _industryLabel;
}

- (UIButton *)talkAttackButton
{
    if (!_talkAttackButton)
    {
        _talkAttackButton = [Utilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-75*kUIScaleBasePlus,CGRectGetMaxY(self.headImage.frame)+22*kUIScaleBasePlus,75*kUIScaleBasePlus,25*kUIScaleBasePlus)
                                          textAligment:UIControlContentHorizontalAlignmentCenter
                                                  text:@"年龄"
                                              textClor:LCHexColor(0x282828)
                                             buttonBGC:[UIColor clearColor]
                                              textFont:13*kUIScaleBasePlus
                                                target:self
                                                method:@selector(talkAttackClick)];
        _talkAttackButton.enabled = NO;

        
    }
    return _talkAttackButton;
}

- (ProgressView *)progressView
{
    if (!_progressView)
    {
        _progressView = [[ProgressView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.headImage.frame)+38*kUIScaleBasePlus,CGRectGetMaxY(self.expressionImageView.frame)+20*kUIScaleBasePlus,200*kUIScaleBasePlus,60*kUIScaleBasePlus)];
    }
    return _progressView;
}

- (UIImageView *)customerImageView
{
    if (!_customerImageView)
    {
        _customerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15*kUIScaleBasePlus,202*kUIScaleBasePlus,200*kUIScaleBasePlus,100*kUIScaleBasePlus)];
        _customerImageView.layer.masksToBounds = YES;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_customerImageView.bounds byRoundingCorners:UIRectCornerTopLeft| UIRectCornerBottomLeft cornerRadii:CGSizeMake(3, 3)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = _customerImageView.bounds;
        maskLayer.path = maskPath.CGPath;
        _customerImageView.layer.mask = maskLayer;
        
    }
    return _customerImageView;
}

- (UIButton *)serviceCustomerButton
{
    if (!_serviceCustomerButton)
    {
        _serviceCustomerButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.headImage.frame)+40*kUIScaleBasePlus, 142*kUIScaleBasePlus,153*kUIScaleBasePlus,36*kUIScaleBasePlus)
                                                   textAligment:UIControlContentHorizontalAlignmentCenter
                                                           text:@"接待"
                                                       textClor:LCHexColor(0xFFFFFF)
                                                      buttonBGC:LCHexColor(0xFF7A17)
                                                       textFont:17*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(serviceCustomer)];
        
        _serviceCustomerButton.layer.masksToBounds = YES;
        _serviceCustomerButton.layer.cornerRadius = _serviceCustomerButton.frame.size.height/2;
        _serviceCustomerButton.titleLabel.numberOfLines = 0;
    }
    return _serviceCustomerButton;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10;
        
        NSString *str = LCUserDefaultsGet(@"bageNum");
        self.bageCount = str.integerValue;
        [self.contentView addSubview:self.expressionImageView];
        [self.contentView addSubview:self.headImage];
        [self.contentView addSubview:self.subTitleLabel];
        [self.contentView addSubview:self.sexImageView];
        [self.contentView addSubview:self.normalButton];
        [self.contentView addSubview:self.industryLabel];
        [self.contentView addSubview:self.progressView];
        [self.contentView addSubview:self.serviceCustomerButton];
    }
    return self;
}


- (void)normalClcik
{
    
}

- (void)talkAttackClick
{
    
}

- (void)serviceCustomer
{
    NSDictionary *pramaters = @{
                                @"id" : self.model.customerId,
                                @"operate" : @"1"
                                };
    
    WeakSelf(self)
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    self.serviceCustomerButton.enabled = NO;
    [infoManager serviceCustomer:pramaters sucess:^(id  _Nonnull acceptData) {
        NSNumber *num = acceptData[@"data"];
        weakSelf.serviceCustomerButton.enabled = YES;
        switch (num.integerValue)
        {
            case 0:
            {
                [SVProgressHUD showWithStatus:@"已结束服务"];
                weakSelf.getCustomerSuccessBlock();

            }
                break;
            case 1:
            {
                if (weakSelf.getCustomerSuccessBlock)
                {
                    NSString *str = LCUserDefaultsGet(@"bageNum");
                    weakSelf.bageCount = str.integerValue;
                    weakSelf.bageCount ++;
                    NSString *bageNum = [NSString stringWithFormat:@"%ld",self.bageCount];
                    LCUserDefaultsSet(bageNum, @"bageNum");
                    LCUserDefaultsSyn;
                    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                    [appDelegate showTabCount:[NSString stringWithFormat:@"%ld",self.bageCount]];
                    weakSelf.getCustomerSuccessBlock();
                    [SVProgressHUD showSuccessWithStatus:@"接客成功"];
                }
            }
                break;
            case 2:
            {
                [SVProgressHUD showWithStatus:@"已被其他导员接客"];
                weakSelf.getCustomerSuccessBlock();
            }
                break;
            default:
                break;
        }
    } fail:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        weakSelf.serviceCustomerButton.enabled = YES;
    }];
}

- (void)setModel:(RealTimeModel *)model
{
    _model = model;

    SummaryModel *smodel = [[SummaryModel alloc]initWithDic:model.summary];
    
    if (smodel.gender.intValue == 0)
    {
        self.sexImageView.image = [UIImage imageNamed:@"sex_gril"];
    }
    else
    {
        self.sexImageView.image = [UIImage imageNamed:@"sex_boy"];
    }
    
    [self.normalButton setTitle:[NSString stringWithFormat:@"%@,%ld",smodel.gender.integerValue == 0 ? @"女" : @"男",smodel.age.integerValue] forState:UIControlStateNormal];

    self.industryLabel.text = smodel.industry;
    
   [self.progressView getinfo:@[[NSString stringWithFormat:@"忠诚度         %.1f",smodel.loyalty.floatValue],[NSString stringWithFormat:@"生命周期     %@",smodel.lifeCycle],[NSString stringWithFormat:@"描述             %@",smodel.label]] isFirstTime:smodel.isFirstTime.integerValue];
    
    self.subTitleLabel.text = model.customerType.integerValue == 1 ? @"新顾客" : @"老顾客";
    
    if (model.customerType.integerValue == 1)
    {
        self.subTitleLabel.textColor = LCHexColor(0x015AE6);
    }
    else
    {
        self.subTitleLabel.textColor = LCHexColor(0xE3413F);
    }
    
 
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    
        switch (model.expression.integerValue)
        {
            case 0:
            {
                self.expressionImageView.image = [UIImage imageNamed:@"jingya"];
            }
                    break;
            case 3:
            {
                self.expressionImageView.image = [UIImage imageNamed:@"kaixin"];
                
            }
                break;
            case 4:
            {
                self.expressionImageView.image = [UIImage imageNamed:@"beishang"];
                    
            }
                break;
            case 5:
            {
                self.expressionImageView.image = [UIImage imageNamed:@"shengqi"];
                
            }
                break;
            case 6:
            {
                self.expressionImageView.image = [UIImage imageNamed:@"pingjing"];
                
            }
                break;
                default:
                    break;
        }
}
@end
